package com.ivan.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ivan.eduservice.entity.EduSubject;
import com.ivan.eduservice.entity.node.SubjectNode;
import com.ivan.eduservice.entity.excel.SubjectData;
import com.ivan.eduservice.listener.SubjectExcelListener;
import com.ivan.eduservice.mapper.EduSubjectMapper;
import com.ivan.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Ivan
 * @since 2020-08-08
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * @Author Ivan
     * @Description 添加课程分类
     * @Date 2020/8/8 14:24
     * @Param 
     * @return 
     */
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @Author Ivan
     * @Description 课程分类列表（树形结构）
     * @Date 2020/8/8 19:51
     * @Param
     * @return
     */
    @Override
    public List<SubjectNode> getAllSubjectNodes() {
        //查询条件
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("parent_id");

        //查询结果
        List<EduSubject> eduSubjects = baseMapper.selectList(wrapper);

        //转换数据
        List<SubjectNode> subjectNodes = new ArrayList<>();
        if (eduSubjects!=null&&eduSubjects.size()>0){
            for (EduSubject subject : eduSubjects) {
                //节点对象
                SubjectNode subjectNode = new SubjectNode();
                //复制字段
                BeanUtils.copyProperties(subject, subjectNode);
                subjectNodes.add(subjectNode);
            }
        }
        return list2Tree(subjectNodes);
    }

    /**
     * @Author Ivan
     * @Description 集合转树形结构
     * @Date 2020/8/8 20:02
     * @Param
     * @return
     */
    public List<SubjectNode> list2Tree(List<SubjectNode> subjectNodes){
        //返回的数据集合
        List<SubjectNode> subjectNodeList = new ArrayList<>();
        //只有两级,所以用两次循环即可,多级时可用递归
        for (SubjectNode subjectNode : subjectNodes){
            if (subjectNode.getParentId().equals("0")){
                //一级节点
                subjectNodeList.add(subjectNode);
                //二级节点
                for (SubjectNode childNode : subjectNodes) {
                    if (childNode.getParentId().equals(subjectNode.getId())){
                        subjectNode.getChildren().add(childNode);
                    }
                }
            }
        }
        return subjectNodeList;
    }
}
